#!/bin/bash
iamwho=$(whoami)
if [ "${iamwho}" = "root" ] ;then
	UBUNTU_CODE=$(grep "^deb" /etc/apt/sources.list | head -1 | awk -F"[ ]" '{print $3}' | awk -F"-" '{print $1}' )
	sed -i "s@http://.*archive.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
	sed -i "s@http://ports.ubuntu.com@http://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
	apt update 
	
	apt install python3-pip gpg -y
	
	echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ ${UBUNTU_CODE} main" >> /etc/apt/sources.list
	apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
	
	su $SUDO_USER
	python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
	pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
else
	echo [ERROR]Run this script with 'sudo $0' for permission reason.
fi
